package com.ygqh.baby.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.CheckStatus;
import com.ygqh.baby.ao.CommentType;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgPublishCommentMapper;
import com.ygqh.baby.model.CommentCountModel;
import com.ygqh.baby.model.YgPublishCommentModel;
import com.ygqh.baby.po.YgMessage;
import com.ygqh.baby.po.YgPublishComment;
import com.ygqh.baby.po.YgPublishCommentExample;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgMessageService;
import com.ygqh.baby.service.YgPublishCommentService;
import com.ygqh.baby.service.YgUserService;
import com.ygqh.baby.service.task.YgAsynClient;
import com.ygqh.baby.service.task.YgAsynServer;

@Service
public class YgPublishCommentServiceImpl implements YgPublishCommentService {

	@Autowired
	private YgPublishCommentMapper ygPublishCommentMapper;
	@Autowired
	private YgMessageService ygMessageService;
	@Autowired
	private YgUserService ygUserService;
	@Autowired
	private YgAsynClient ygAsynClient;
	@Autowired
	private YgAsynServer YgAsynServer;

	@Autowired
	private DocumentConstant documentConstant;

	@Override
	public List<YgPublishComment> find() {
		YgPublishCommentExample example = new YgPublishCommentExample();
		example.createCriteria().andAuditStatusNotEqualTo(CheckStatus.Reject.name());
		return ygPublishCommentMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgPublishCommentExample example = new YgPublishCommentExample();
		example.createCriteria().andAuditStatusNotEqualTo(CheckStatus.Reject.name());
		return ygPublishCommentMapper.countByExample(example);
	}

	@Override
	public YgPublishComment findById(Long id) {
		return ygPublishCommentMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgPublishComment ygPublishComment) {
		return ygPublishCommentMapper.insertSelective(ygPublishComment);
	}

	@Override
	public int update(YgPublishComment ygPublishComment) {
		return ygPublishCommentMapper.updateByPrimaryKeySelective(ygPublishComment);
	}

	@Override
	public ResultSet<YgPublishComment> search(QueryInfo queryInfo, String q, Long id, String checkStatus, Date startTime, Date endTime) {
		List<YgPublishComment> list = ygPublishCommentMapper.selectSuper(queryInfo, q, id, checkStatus, startTime, endTime);
		int count = ygPublishCommentMapper.countSuper(q, id, checkStatus, startTime, endTime);
		ResultSet<YgPublishComment> resultSet = new ResultSet<YgPublishComment>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		int count = 0;
		for (Long id : ids) {
			YgPublishComment ygPublishComment = this.findById(id);
			ygPublishComment.setAuditStatus(CheckStatus.Reject);
			count += ygPublishCommentMapper.updateByPrimaryKeySelective(ygPublishComment);
		}

		return count;
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgPublishComment ygPublishComment) {
		int r = 0;
		if (ygPublishComment.getId() != null) {
			r = this.update(ygPublishComment);
		} else {
			r = this.save(ygPublishComment);
		}
		return r;
	}

	@Override
	public List<CommentCountModel> findCountByPublishIds(CommentType commentType, Long[] publishIds) {
		List<CommentCountModel> counts = ygPublishCommentMapper.findCountByPublishIds(commentType.name(), publishIds);

		return counts;
	}

	@Override
	public List<YgPublishCommentModel> findCommentByPublishId(Long publishId) {
		return ygPublishCommentMapper.findCommentByPublishId(publishId);
	}

	@Override
	public void pushNice(YgPublishComment comment, Long receiverId) {
		YgPublishCommentExample example = new YgPublishCommentExample();
		example.createCriteria().andAuditStatusEqualTo(CheckStatus.Pass.name()).andPublishTypeEqualTo(CommentType.Praise.name())
				.andPublishIdEqualTo(comment.getPublishId()).andUserIdEqualTo(comment.getUserId());

		List<YgPublishComment> list = ygPublishCommentMapper.selectByExample(example);
		// 如果没赞过，则添加记录
		if (list.isEmpty()) {
			ygPublishCommentMapper.insertSelective(comment);
			sendMessage(comment, receiverId);
		} else {
			// 赞过则删除
			ygPublishCommentMapper.deleteByPrimaryKey(list.get(0).getId());
		}
	}

	public void sendMessage(YgPublishComment comment, Long receiverId) {
		YgMessage message = new YgMessage();
		YgUser user = ygUserService.findById(comment.getUserId());
		String nickName = user.getNickName();
		message.setCreateBy(nickName);
		message.setCreateTime(new Date());
		message.setIsRead(false);
		String title = "";
		if (comment.getPublishType().equals(CommentType.Comment)) {
			title = nickName + "" + documentConstant.MESSAGE_COMMENT;
			message.setMessageType("Comment");
		} else if (comment.getPublishType().equals(CommentType.Praise)) {
			title = nickName + "" + documentConstant.MESSAGE_NICE;
			message.setMessageType("Praise");
		}
		message.setMessageTitle(title);
		message.setContent(comment.getContent());
		message.setReleationId(comment.getPublishId());
		message.setUserId(receiverId);
		message.setFromUserId(comment.getUserId());
		message.setStatus(DataStatus.Valid);
		ygMessageService.save(message);
		// 发送微信消息
		ygAsynClient.sendNewReviewMsg(comment.getPublishId(), comment.getUserId(), nickName, comment.getPublishType());
	}

	@Override
	public Long[] findPublishIdsByUser(Long userId) {

		YgPublishCommentExample example = new YgPublishCommentExample();
		example.createCriteria().andAuditStatusEqualTo(CheckStatus.Pass.name()).andPublishTypeEqualTo(CommentType.Praise.name()).andUserIdEqualTo(userId);
		List<YgPublishComment> comments = ygPublishCommentMapper.selectByExample(example);
		if (comments.isEmpty())
			return new Long[] {};

		Long[] publishIds = new Long[comments.size()];

		for (int i = 0; i < comments.size(); i++) {
			publishIds[i] = comments.get(i).getPublishId();
		}

		return publishIds;
	}

	@Override
	public int updateBatchChechStatus(String checkBy, String checkStatus, Long[] ids) {
		return ygPublishCommentMapper.updateBatchChechStatus(checkBy, checkStatus, ids);
	}

	@Override
	public List<YgPublishComment> findByPublishId(Long publishId) {
		return ygPublishCommentMapper.selectByPublishId(publishId);
	}

}
